#include "stdafx.h"
#include "Mutithreads.h"
// this_thread::yield example

std::atomic<bool> CMutithreads::ready;
CMutithreads::CMutithreads()
{
}

CMutithreads::~CMutithreads()
{
}

int CMutithreads::CoutThreads()
{
	std::thread threads[10];
	std::cout << "race of 10 threads that count to 1 million:\n";
	for (int i = 0; i < 10; ++i) threads[i] = std::thread(count1m, i);
	ready = true;               // go!
	for (auto& th : threads) th.join();
	std::cout << '\n';

	return 0;
}
// std::atomic

void  CMutithreads::count1m(int id) {
	while (!ready) {             // wait until main() sets ready...
		std::this_thread::yield();
	}
	for (volatile int i = 0; i < 1000000; ++i) {}
	std::cout << id;
}